package com.atues.thread.state;

//测试stop
//1. 建议线程正常停止

//3.不适用stop或者destory等过时或者jdk不建议使用的方法；
public class TestStop implements Runnable {
    private boolean flag = true;

    //1. 设置一个标志位
    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("run thread" + i++);
        }

    }

    public void stop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        Thread thread = new Thread(testStop);
        thread.start();

        for (int i = 0; i < 100; i++) {
            System.out.println("main" + i);
            if (i == 20) {
                testStop.stop(); //设置停止标志
                System.out.println("线程停止了");
            }
        }
    }
}
